package com.sumover.controller;

import com.sumover.POJO.*;
import com.sumover.Service.ArticleService;
import com.sumover.Service.UserService;
import com.sumover.util.ResultVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * 所有的文章的API
 * 包括新闻, 攻略,
 */
@RequestMapping("/article")
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取所有的新闻
     *
     * @return
     */
    @RequestMapping("/getAllNews")
    public ResultVo getAllNews() {
        List<News> allNews = articleService.getAllNews();
        List<List<Object>> result = new LinkedList<>();
        for (News news : allNews) {
            List turple = new LinkedList();
            turple.add(news);// 新闻
            turple.add(articleService.getNewsArticle(news)); // 新闻的文章
            turple.add(articleService.getArticleUser(news.getAid())); // 新闻的编写者
            result.add(turple);
        }
        return ResultVo.success("return all news", result);
    }

    @RequestMapping("/getNewsDetail")
    public ResultVo getNewsDetail(@RequestBody News news) {
        Article newsArticle = articleService.getNewsArticle(news);  // 新闻的文章
        ArticleDetail articleDetail = articleService.getArticleDetail(newsArticle); // 新闻的详情
        User articleUser = articleService.getArticleUser(news.getAid());// 新闻的编写者
        List result = new LinkedList();
        result.add(newsArticle);
        result.add(articleDetail);
        result.add(articleUser);
        return ResultVo.success("news detail returned", result);
    }

    /**
     * 获取所有的攻略
     *
     * @return
     */
    @RequestMapping("/getAllStrategy")
    public ResultVo getAllStrategy() {
        List<GameStrategy> allStrategy = articleService.getAllStrategy();
        List<List> result = new LinkedList<>();
        for (GameStrategy gameStrategy : allStrategy) {
            List turple = new LinkedList();
            turple.add(gameStrategy);//游戏攻略
            turple.add(articleService.getStrategyArticle(gameStrategy));//游戏攻略文章
            turple.add(articleService.getArticleUser(gameStrategy.getAid()));
            result.add(turple);
        }
        return ResultVo.success("return all game strategy", result);
    }

    /**
     * 根据游戏获取攻略
     *
     * @param game
     * @return
     */
    @RequestMapping("/getStrategiesByGame")
    public ResultVo getStrategiesByGame(@RequestBody Game game) {
        List<GameStrategy> strategiesByGame = articleService.getStrategiesByGame(game);
        List<List> result = new LinkedList<>();
        for (GameStrategy gameStrategy : strategiesByGame) {
            List turple = new LinkedList();
            turple.add(gameStrategy);// 第0个元素, 游戏攻略
            turple.add(articleService.getStrategyArticle(gameStrategy));//第1个元素, 游戏文章
            turple.add(articleService.getArticleUser(gameStrategy.getAid()));//第2个元素, 游戏编写者的用户名
            result.add(turple);
        }
        return ResultVo.success("return strategies by game", result);
    }

    @Autowired
    private UserService userService;

    /**
     * 获得游戏评测
     *
     * @param game
     * @return
     */
    @RequestMapping("/getGameEvaluations")
    public ResultVo getGameEvaluations(@RequestBody Game game) {
        List<GameEvaluation> gameEvaluations = articleService.getGameEvaluations(game);
        List<List> result = new LinkedList<List>();
        for (GameEvaluation gameEvaluation : gameEvaluations) {
            List turple = new LinkedList();
            Article evaluationsArticle = articleService.getEvaluationsArticle(gameEvaluation);
            turple.add(gameEvaluation);//游戏评测
            turple.add(evaluationsArticle);//评测内容
            turple.add(articleService.getArticleUser(evaluationsArticle.getArticleid()));//评测的发布者
            result.add(turple);
        }
        return ResultVo.success("game evaluations returned", gameEvaluations);
    }


    /**
     * 获得新闻的文章
     *
     * @param news
     * @return
     */
    @RequestMapping("/getNewsArticle")
    public ResultVo getNewsArticle(@RequestBody News news) {
        Article newsArticle = articleService.getNewsArticle(news);
        return ResultVo.success("article returned", newsArticle);
    }

    /**
     * 获得攻略的文章
     *
     * @param gameStrategy
     * @return
     */
    @RequestMapping("/getStrategyArticle")
    public ResultVo getStrategyArticle(@RequestBody GameStrategy gameStrategy) {
        Article strategyArticle = articleService.getStrategyArticle(gameStrategy);
        return ResultVo.success("article returned", strategyArticle);
    }

    /**
     * 获得评测的文章
     *
     * @param gameEvaluation
     * @return
     */
    @RequestMapping("/getEvaluationArticle")
    public ResultVo getEvaluationArticle(@RequestBody GameEvaluation gameEvaluation) {
        Article evaluationArticle = articleService.getEvluationArticle(gameEvaluation);
        return ResultVo.success("article returned", evaluationArticle);
    }

    /**
     * 获得文章详细信息
     *
     * @param article
     * @return
     */
    @RequestMapping("/getArticleDetail")
    public ResultVo getArticleDetail(@RequestBody Article article) {
        ArticleDetail articleDetail = articleService.getArticleDetail(article);
        return ResultVo.success("article detail returned", articleDetail);
    }

    @RequestMapping("/getArticleById")
    public ResultVo getArticleById(@RequestBody Integer articleId) {
        Article article = articleService.getArticle(articleId);
        return ResultVo.success("article returned", article);
    }

    @RequestMapping("/getArticlePublisher")
    public ResultVo getArticlePublisher(@RequestBody Article article) {
        User articlePublisher = articleService.getArticlePublisher(article);
        return ResultVo.success("article publisher returned", articlePublisher);
    }

    @RequestMapping("/addNewEvaluation")
    public ResultVo addNewEvaluation(@RequestBody Game game, @RequestBody Integer score,
                                     @RequestBody String evaluationContent, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResultVo.error("user not login");
        }
        if (!articleService.evaluationExist(game, loginUser))
            articleService.addNewEvaluation(game, score, evaluationContent, loginUser);
        else return ResultVo.error("evaluation already exist!");
        return ResultVo.success("evaluation add success!", null);
    }
}
